<!--
<template>
  <div class="wrapper">
    <div style="margin: 200px auto; background-color: #fff; width: 350px; height: 280px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>酒店后台登录</b></div>
      <el-form :model="admin" :rules="rules" ref="adminForm">
        <el-form-item prop="username">
          <el-input size="medium" prefix-icon="el-icon-user" v-model="admin.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" prefix-icon="el-icon-lock" show-password
                    v-model="admin.password"></el-input>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      admin: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    login() {
      this.$refs.adminForm.validate(async valid => {
        if (valid) {
          try {
            const response = await axios.post(
                "http://localhost:29000/passport/login",
                this.admin
            );
            if (response.data.code === '200') {
              localStorage.setItem("admin", JSON.stringify(response.data.data));
              this.$router.push("/");
              this.$message.success("登录成功");
            } else {
              this.$message.error(response.data.msg || "登录失败");
            }
          } catch (error) {
            console.error(error);
            this.$message.error("登录失败，请稍后重试");
          }
        } else {
          return false;
        }
      });
    }
  }
};

</script>

<style scoped>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right,#d793d9, #82b9e7);
  overflow: hidden;
}
</style>
-->
<template>
  <div>
    <!-- 整个页面需要显示内容的区域 -->
    <div class="container">
      <div style="margin-top: 200px">
        <!-- 顶部区域 -->
        <div class="header">
          <h1>酒店管理系统登录</h1>
        </div>
        <!-- 登录表单 -->
        <div class="login-form">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
            <el-form-item label="" prop="username">
              <el-input v-model="ruleForm.username">
                <template slot="prepend"><i class="el-icon-user-solid"/></template>
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input v-model="ruleForm.password">
                <template slot="prepend"><i class="el-icon-s-ticket"/></template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width: 100%;"
                         @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <!-- 版权信息 -->
      <div class="copyright">
        <p>Copyright &copy; A酒店 - 四川成都 - 酒店管理系统</p>
      </div>
    </div>
  </div>
</template>

<script>
import BaseUrl from "@/http/BaseUrl";
import SimpleAxios from "@/http/SimpleAxios";

export default {
  data() {
    return {
      ruleForm: {
        username: 'root',
        password: '123456'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '用户名的长度必须是 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '密码的长度必须是 4 到 15 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:29000/passport/login";
          console.log('url = ' + url);

          SimpleAxios.post(url, this.ruleForm).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              let loginInfo = jsonResult.data;
              localStorage.setItem('loginInfo', JSON.stringify(loginInfo));
              this.$router.push('/zwt/index');
            } else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }
          });
        } else {
          return false;
        }
      });
    }
  }
}
</script>

<style>
body {
  height: 100vh;
  background-image: linear-gradient(to bottom right,#d793d9, #82b9e7);  background-repeat: no-repeat;
}

.container {
  width: 430px;
  margin: 0 auto;
}

.header {
  margin-top: 50px;
}

.header h1 {
  text-align: center;
  color: #fff;
}

.login-form {
  background: #fff;
  border-radius: 10px;
  padding: 50px 50px 20px 50px;
}

.copyright {
  margin-top: 20px;
}

.copyright p {
  text-align: center;
  font-size: 11px;
  color: #fff;
}
</style>
